package admin

import (
	"encoding/json"
	"go-cmdb/models"
)

type SettingController struct {
	AdminController
}

func NewSettingController() *SettingController {
	ctrl := &SettingController{}
	return ctrl
}

func (ctrl *SettingController) GetAll() {

	m := models.NewSetting().GetMap()
	ctrl.OK(m)
	return

}
func (ctrl *SettingController) Index() {

	ctrl.ViewData(map[string]interface{}{
		"title": "参数配置",
	})
	return

}

func (ctrl *SettingController) Set() {

	var req models.SetReq
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	auth, _ := ctrl.GetAuthUser()

	if err := models.NewSetting().SaveBatch(map[string]string{
		req.Field: req.Value,
	}, auth.UserID); err != nil {

		ctrl.ERROR(err.Error())
		return

	}
	ctrl.OK(nil)
	return

}

func (ctrl *SettingController) SetMap() {

	var req map[string]string
	if err := json.Unmarshal(ctrl.Ctx.Input.RequestBody, &req); err != nil {
		ctrl.ERROR(err.Error())
		return
	}
	auth, _ := ctrl.GetAuthUser()

	if err := models.NewSetting().SaveBatch(req, auth.UserID); err != nil {

		ctrl.ERROR(err.Error())
		return

	}
	ctrl.OK(nil)
	return

}
